package com.chlp.core.framework.controller;

import com.chlp.core.framework.service.CommonCurdService;
import com.chlp.core.framework.utils.CommonCurdPageBean;
import com.chlp.core.framework.utils.CommonCurdResultBean;
import com.chlp.core.framework.utils.MulTableQuery;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

/**
 * @author 陈礼鹏
 * @date 2020/1/16 17:23
 */
public class CommonCurdController<T> {

	private final CommonCurdService commonCurdService;

	public CommonCurdController(CommonCurdService commonCurdService) {
		this.commonCurdService = commonCurdService;
	}

	@ApiOperation(value = "通用接口：单表查询(分页)")
	@PostMapping(value = "/common/queryByConditionPage")
	public CommonCurdResultBean<MulTableQuery.Pagination<T>> commonQueryByConditionPage(@RequestBody CommonCurdPageBean<T> commonCurdPageBean) {
		CommonCurdResultBean<MulTableQuery.Pagination<T>> resultBean = new CommonCurdResultBean<>(true, "");
		Integer[] page = MulTableQuery.handlePageParameter(commonCurdPageBean);
		T t = commonCurdPageBean.getT();
		resultBean.setObject((MulTableQuery.Pagination<T>) commonCurdService.queryAnyTableByConditionPage(t, page[0], page[1]));
		return resultBean;
	}

	@ApiOperation(value = "通用接口：单表查询(不分页)")
	@PostMapping(value = "/common/queryByCondition")
	public CommonCurdResultBean<List<T>> commonQueryByCondition(@RequestBody T t) {
		CommonCurdResultBean<List<T>> resultBean = new CommonCurdResultBean<>(true, "");
		resultBean.setObject((List<T>) commonCurdService.queryAnyTableByCondition(t));
		return resultBean;
	}

	@ApiOperation(value = "通用接口：根据条件查询出一条记录")
	@PostMapping(value = "/common/queryOneByCondition")
	public CommonCurdResultBean<T> commonQueryOneByCondition(@RequestBody T t) {
		CommonCurdResultBean<T> resultBean = new CommonCurdResultBean<>(true, "");
		resultBean.setObject((T) commonCurdService.queryOneAnyTableByCondition(t));
		return resultBean;
	}

	@ApiOperation(value = "通用接口：单表新增(对象属性不为空)")
	@PostMapping(value = "/common/insertOne")
	public CommonCurdResultBean<Integer> commonInsertOne(@RequestBody T t) {
		CommonCurdResultBean<Integer> resultBean = new CommonCurdResultBean<>(true, "");
		resultBean.setObject(commonCurdService.insertOne(t));
		return resultBean;
	}

	@ApiOperation(value = "通用接口：单表批量新增(对象属性不为空)")
	@PostMapping(value = "/common/insertList")
	public CommonCurdResultBean<T> commonInsertList(@RequestBody List<T> t) {
		CommonCurdResultBean<T> resultBean = new CommonCurdResultBean<>(true, "");
		commonCurdService.insertList((List<Object>) t);
		return resultBean;
	}

	@ApiOperation(value = "通用接口：单表删除(根据对象属性不为空做为查询条件)")
	@PostMapping(value = "/common/deleteByEntity")
	public CommonCurdResultBean<T> commonDeleteByEntity(@RequestBody T t) {
		CommonCurdResultBean<T> resultBean = new CommonCurdResultBean<>(true, "");
		commonCurdService.deleteByEntity(t);
		return resultBean;
	}

	@ApiOperation(value = "通用接口：单表删除(根据id)")
	@PostMapping(value = "/common/updateById")
	public CommonCurdResultBean<T> commonUpdateById(@RequestBody T t) {
		CommonCurdResultBean<T> resultBean = new CommonCurdResultBean<>(true, "");
		commonCurdService.updateById(t);
		return resultBean;
	}
}